home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / PROGRAMMING / ZIPLIB / c / zutil < prev   
Text File  |  1996-07-31  |  5KB  |  197 lines

  1. /* zutil.c -- target dependent utility functions for the compression library
  2.  * Copyright (C) 1995-1996 Jean-loup Gailly.
  3.  * For conditions of distribution and use, see copyright notice in zlib.h 
  4.  */
  5.  
  6. /* $Id: zutil.c,v 1.15 1996/05/23 17:11:36 me Exp $ */
  7.  
  8. #include <stdio.h>
  9.  
  10. #include "zutil.h"
  11.  
  12. struct internal_state      {int dummy;}; /* for buggy compilers */
  13.  
  14. #ifndef STDC
  15. extern void exit OF((int));
  16. #endif
  17.  
  18. const char *z_errmsg[10] = {
  19. "need dictionary",     /* Z_NEED_DICT       2  */
  20. "stream end",          /* Z_STREAM_END      1  */
  21. "",                    /* Z_OK              0  */
  22. "file error",          /* Z_ERRNO         (-1) */
  23. "stream error",        /* Z_STREAM_ERROR  (-2) */
  24. "data error",          /* Z_DATA_ERROR    (-3) */
  25. "insufficient memory", /* Z_MEM_ERROR     (-4) */
  26. "buffer error",        /* Z_BUF_ERROR     (-5) */
  27. "incompatible version",/* Z_VERSION_ERROR (-6) */
  28. ""};
  29.  
  30.  
  31. char *zlibVersion()
  32. {
  33.     return ZLIB_VERSION;
  34. }
  35.  
  36. void z_error (m)
  37.     char *m;
  38. {
  39.     fprintf(stderr, "%s\n", m);
  40.     exit(1);
  41. }
  42.  
  43. #ifndef HAVE_MEMCPY
  44.  
  45. void zmemcpy(dest, source, len)
  46.     Bytef* dest;
  47.     Bytef* source;
  48.     uInt  len;
  49. {
  50.     if (len == 0) return;
  51.     do {
  52.         *dest++ = *source++; /* ??? to be unrolled */
  53.     } while (--len != 0);
  54. }
  55.  
  56. void zmemzero(dest, len)
  57.     Bytef* dest;
  58.     uInt  len;
  59. {
  60.     if (len == 0) return;
  61.     do {
  62.         *dest++ = 0;  /* ??? to be unrolled */
  63.     } while (--len != 0);
  64. }
  65. #endif
  66.  
  67. #ifdef __TURBOC__
  68. #if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
  69. /* Small and medium model in Turbo C are for now limited to near allocation
  70.  * with reduced MAX_WBITS and MAX_MEM_LEVEL
  71.  */
  72. #  define MY_ZCALLOC
  73.  
  74. /* Turbo C malloc() does not allow dynamic allocation of 64K bytes
  75.  * and farmalloc(64K) returns a pointer with an offset of 8, so we
  76.  * must fix the pointer. Warning: the pointer must be put back to its
  77.  * original form in order to free it, use zcfree().
  78.  */
  79.  
  80. #define MAX_PTR 10
  81. /* 10*64K = 640K */
  82.  
  83. local int next_ptr = 0;
  84.  
  85. typedef struct ptr_table_s {
  86.     voidpf org_ptr;
  87.     voidpf new_ptr;
  88. } ptr_table;
  89.  
  90. local ptr_table table[MAX_PTR];
  91. /* This table is used to remember the original form of pointers
  92.  * to large buffers (64K). Such pointers are normalized with a zero offset.
  93.  * Since MSDOS is not a preemptive multitasking OS, this table is not
  94.  * protected from concurrent access. This hack doesn't work anyway on
  95.  * a protected system like OS/2. Use Microsoft C instead.
  96.  */
  97.  
  98. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  99. {
  100.     voidpf buf = opaque; /* just to make some compilers happy */
  101.     ulg bsize = (ulg)items*size;
  102.  
  103.     /* If we allocate less than 65520 bytes, we assume that farmalloc
  104.      * will return a usable pointer which doesn't have to be normalized.
  105.      */
  106.     if (bsize < 65520L) {
  107.         buf = farmalloc(bsize);
  108.         if (*(ush*)&buf != 0) return buf;
  109.     } else {
  110.         buf = farmalloc(bsize + 16L);
  111.     }
  112.     if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
  113.     table[next_ptr].org_ptr = buf;
  114.  
  115.     /* Normalize the pointer to seg:0 */
  116.     *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
  117.     *(ush*)&buf = 0;
  118.     table[next_ptr++].new_ptr = buf;
  119.     return buf;
  120. }
  121.  
  122. void  zcfree (voidpf opaque, voidpf ptr)
  123. {
  124.     int n;
  125.     if (*(ush*)&ptr != 0) { /* object < 64K */
  126.         farfree(ptr);
  127.         return;
  128.     }
  129.     /* Find the original pointer */
  130.     for (n = 0; n < next_ptr; n++) {
  131.         if (ptr != table[n].new_ptr) continue;
  132.  
  133.         farfree(table[n].org_ptr);
  134.         while (++n < next_ptr) {
  135.             table[n-1] = table[n];
  136.         }
  137.         next_ptr--;
  138.         return;
  139.     }
  140.     ptr = opaque; /* just to make some compilers happy */
  141.     z_error("zcfree: ptr not found");
  142. }
  143. #endif
  144. #endif /* __TURBOC__ */
  145.  
  146.  
  147. #if defined(M_I86) && !(defined(__WATCOMC__) && defined(__386__))
  148. /* Microsoft C */
  149.  
  150. #  define MY_ZCALLOC
  151.  
  152. #if (!defined(_MSC_VER) || (_MSC_VER < 600))
  153. #  define _halloc  halloc
  154. #  define _hfree   hfree
  155. #endif
  156.  
  157. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  158. {
  159.     if (opaque) opaque = 0; /* to make compiler happy */
  160.     return _halloc((long)items, size);
  161. }
  162.  
  163. void  zcfree (voidpf opaque, voidpf ptr)
  164. {
  165.     if (opaque) opaque = 0; /* to make compiler happy */
  166.     _hfree(ptr);
  167. }
  168.  
  169. #endif /* MSC */
  170.  
  171.  
  172. #ifndef MY_ZCALLOC /* Any system without a special alloc function */
  173.  
  174. #ifndef STDC
  175. extern voidp  calloc OF((uInt items, uInt size));
  176. extern void   free   OF((voidpf ptr));
  177. #endif
  178.  
  179. voidpf zcalloc (opaque, items, size)
  180.     voidpf opaque;
  181.     unsigned items;
  182.     unsigned size;
  183. {
  184.     if (opaque) items += size - size; /* make compiler happy */
  185.     return (voidpf)calloc(items, size);
  186. }
  187.  
  188. void  zcfree (opaque, ptr)
  189.     voidpf opaque;
  190.     voidpf ptr;
  191. {
  192.     free(ptr);
  193.     if (opaque) return; /* make compiler happy */
  194. }
  195.  
  196. #endif /* MY_ZCALLOC */
  197.